package com.ruoyi.hkCamera.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.ruoyi.common.core.domain.dto.DownloadVideoDTO;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.hkCamera.common.HCNetSDK;
import com.ruoyi.hkCamera.config.VcrParamsConfig;
import com.ruoyi.hkCamera.utils.HcUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.ruoyi.hkCamera.config.AlarmConfig.*;

@Slf4j
@Service
public class VideoService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private VcrParamsConfig vcrParamsConfig;

    /**
     * 过车视频下载
     */
    public void downloadVideo(DownloadVideoDTO dto) {
        String videoName = dto.getVideoName();
        String videoDirs = dto.getVideoDirs();
        String checkNo = dto.getCheckNo();
        String laneNo = dto.getLaneNo();
        Date checkTime = dto.getCheckTime();
        log.info("过车视频下载 开始 checkNo：{} laneNo：{} checkTime：{}", checkNo, laneNo, checkTime);
        String ip = vcrParamsConfig.getIp();
        int userID = devIp2UserIdMap.get(ip);
        if (userID != -1) {
            Date startTime = DateUtil.offsetSecond(checkTime, vcrParamsConfig.getOffsetStart());
            Date stopTime = DateUtil.offsetSecond(checkTime, vcrParamsConfig.getOffsetStop());

            HCNetSDK.NET_DVR_PLAYCOND net_dvr_play = new HCNetSDK.NET_DVR_PLAYCOND();
            net_dvr_play.read();
            int dwChannel = vcrParamsConfig.getDwChannel(laneNo);
            net_dvr_play.dwChannel = dwChannel + 32;
            net_dvr_play.struStartTime = HcUtils.getHkTime(startTime);
            net_dvr_play.struStopTime = HcUtils.getHkTime(stopTime);
            net_dvr_play.write();
            File file = new File(videoDirs);
            if (!file.exists()) {
                file.mkdirs();
            }
            String sFileName = videoDirs + videoName;
            log.info("过车视频下载 路径：" + sFileName);
            int hPlayback = hCNetSDK.NET_DVR_GetFileByTime_V40(userID, sFileName, net_dvr_play);
            log.info("过车视频下载 checkNo：{} hPlayback：{}", checkNo, hPlayback);
            if (hPlayback >= 0) {
                boolean b_PlayBack = hCNetSDK.NET_DVR_PlayBackControl_V40(hPlayback, HCNetSDK.NET_DVR_PLAYSTART, null, 0, null, null);
                if (!b_PlayBack) {
                    log.error("过车视频下载 checkNo：{}, 失败码：{}", checkNo, hCNetSDK.NET_DVR_GetLastError());
                }
                log.info("过车视频下载 checkNo：{}, 开始时间：{}", checkNo, DateUtil.now());
                if (!redisCache.hasKey("downloadVideo")) {
                    Map<String, DownloadVideoDTO> dataMap = new HashMap<>();
                    redisCache.setCacheMap("downloadVideo", dataMap);
                }
                redisCache.setCacheMapValue("downloadVideo", String.valueOf(hPlayback), dto);
                ThreadUtil.sleep(1000);
            } else {
                log.error("过车视频下载 checkNo：{}, 失败码：{}", checkNo, hCNetSDK.NET_DVR_GetLastError());
            }
        }
    }

}
